package com.star.files.controller;


import com.star.files.dto.UploadUrlDTO;
import com.star.files.dto.UploadUrlsDTO;
import com.star.files.service.FilesService;
import com.star.files.vo.UploadResult;
import com.star.files.vo.UploadResults;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 文件管理
 *
 * @author yangzj
 * @order 2
 */
@RequestMapping("/files")
@RestController
public class FilesController {

    private final FilesService filesService;

    public FilesController(FilesService filesService) {
        this.filesService = filesService;
    }

    /**
     * 单文件上传
     *
     * @param file         文件
     * @param fileCategory 文件分类
     * @param keepFileName 保持文件名
     * @return {@link UploadResult}
     */
    @PostMapping("/upload")
    public UploadResult upload(MultipartFile file, @RequestParam String fileCategory, Boolean keepFileName) {
        return new UploadResult(filesService.upload(file, fileCategory, keepFileName));
    }

    /**
     * 多文件上传
     *
     * @param files        文件
     * @param fileCategory 文件分类
     * @param keepFileName 保持文件名
     * @return {@link UploadResults}
     */
    @PostMapping("/uploads")
    public UploadResults uploads(MultipartFile[] files, @RequestParam String fileCategory, Boolean keepFileName) {
        return new UploadResults(filesService.uploads(files, fileCategory, keepFileName));
    }

    /**
     * url单文件上传
     *
     * @param dto dto
     * @return {@link UploadResult}
     */
    @PostMapping("/upload-url")
    public UploadResult upload(@Valid @RequestBody UploadUrlDTO dto) {
        return new UploadResult(filesService.upload(dto.getUrl(), dto.getFileCategory(), dto.getKeepFileName()));
    }

    /**
     * url多文件上传
     *
     * @param dto 入参
     * @return {@link UploadResults}
     */
    @PostMapping("/upload-urls")
    public UploadResults uploads(@Valid @RequestBody UploadUrlsDTO dto) {
        return new UploadResults(filesService.uploads(dto.getUrls(), dto.getFileCategory(), dto.getKeepFileName()));
    }

    /**
     * 文件下载/访问
     *
     * @param fileCategory 文件类别
     * @param id           id
     * @param response     响应
     */
    @GetMapping("/{fileCategory}/{month}/{id:.+}")
    public void download(@PathVariable String fileCategory, @PathVariable String month, @PathVariable String id
            , HttpServletRequest request, HttpServletResponse response) {
        filesService.download("/" + fileCategory + "/" + month + "/" + id, request, response);
    }

    /**
     * 文件下载/访问
     *
     * @param fileCategory 文件类别
     * @param id           id
     * @param response     响应
     */
    @GetMapping("/{fileCategory}/{month}/{id}/{fileName:.+}")
    public void download(@PathVariable String fileCategory, @PathVariable String month, @PathVariable String id
            , @PathVariable String fileName, HttpServletRequest request, HttpServletResponse response) {
        filesService.download("/" + fileCategory + "/" + month + "/" + id + "/" + fileName, request, response);
    }
}
